package com.nhvu.compoundword;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nhvu.compoundword.library.Common;
import com.nhvu.compoundword.library.MyGlobal;
import com.startapp.android.publish.StartAppSDK;

/**
 * Main activity
 * 
 * @author Andy
 * 
 */
@SuppressLint("InflateParams")
public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Init StartApp
		StartAppSDK.init(this, MyGlobal.StartApp.DEV_ID,
				MyGlobal.StartApp.APP_ID, true);
		// Set full screen
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		TextView tvwShare = (TextView) findViewById(R.id.tvwShare);
		tvwShare.setPaintFlags(tvwShare.getPaintFlags()
				| Paint.UNDERLINE_TEXT_FLAG);
	}

	/**
	 * Play single player
	 */
	public void playSinglePlayer(View view) {
		Intent intent = new Intent(this, SinglePlayerActivity.class);
		startActivity(intent);
		Common.closeApp(this);
	}

	/**
	 * Show multil players selection
	 */
	public void showMultilPlayerSelection(View view) {
		LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View popupView = layoutInflater.inflate(R.layout.popup_window, null);

		final PopupWindow popupWindow = new PopupWindow(popupView,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		Button btnDismiss = (Button) popupView.findViewById(R.id.btnExit);
		btnDismiss.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});

		Button btnNewGame = (Button) popupView.findViewById(R.id.btnNewGame);
		btnNewGame.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				playMultilPlayer(true);
			}
		});

		Button btnContinue = (Button) popupView.findViewById(R.id.btnContinue);
		btnContinue.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				playMultilPlayer(false);
			}
		});
		popupWindow.showAtLocation(this.findViewById(R.id.layoutMain),
				Gravity.CENTER, 0, 0);
	}

	/**
	 * Play multil player
	 * 
	 * @param playState
	 *            true is new game, else continue
	 */
	public void playMultilPlayer(Boolean playState) {
		Intent intent = new Intent(this, MultilPlayerResultActivity.class);
		intent.putExtra(MyGlobal.Params.PLAY_STATE, playState);
		startActivity(intent);
		Common.closeApp(this);
	}

	public void exitGame(View view) {
		System.exit(0);
	}

	public void shareGameUrl(View view) {
		Intent share = new Intent(android.content.Intent.ACTION_SEND);
		share.setType("text/plain");
		share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		share.putExtra(Intent.EXTRA_SUBJECT,
				getResources().getString(R.string.text_shared_subject));
		share.putExtra(Intent.EXTRA_TEXT,
				getResources().getString(R.string.text_shared_link));
		startActivity(Intent.createChooser(share,
				getResources().getString(R.string.text_shared_title)));
	}

}
